#include "../begonia.h"
#include "../base/bullet.h"
#include "../base/scene.h"

bool aikeydown[7];
double threshold=1.2;
double corek=1.0,coreb=5.0;
double tshootd=5.0;
double tardist2=5000.0;

//AI Player
//Current Problem: doesn't know how to evade 2 coming bullets
void aiDecision()
{
    int dir;
    double r0,dx,dy,v,d1,d2,q0,qmin,tmin,tshoot;
    double dxc,dyc;
    double tshootmax=(cbwmax-cbwmin)/cbdw*smul;
    double cbhmin=cbhmax-tshootmax*cbdw/smul;
    bullet *boss;
    qmin=threshold;
    tmin=tshootmax;
    dir=0;
    if(shooting)
    {
        boss=f.head.pre;
        dx=cbx-boss->x;
        dy=cby-boss->y;
        dxc=abs(dx)-boss->r/3.0;
        dyc=abs(dy)-boss->r/3.0;
        if(dxc>cbw/2.0) if(dx>0) dir=1; else dir=5;
        else if(dyc>cbh/2.0) if(dy>0) dir=7; else dir=3;
        else aikeydown[1]=0;
        //sprintf(tests,"cbx=%.2f cby=%.2f",cbx,cby);
    }
    else
    {
        for(bullet *k=f.head.next;k!=&f.head;k=k->next)
        {
            r0=abs((k->r-corermax)*corek)+coreb;
            v=sqrt(sqr(k->vx)+sqr(k->vy));
            //sprintf(s,"v=%f",v);
            //MessageBox(0,s,"test",0);
            if(v>0.0)
            {
                dx=f.px-k->x;
                dy=f.py-k->y;
                d2=(dx*k->vy-dy*k->vx)/v;
                if(abs(d2)<r0)
                {
                    d1=(dx*k->vx+dy*k->vy)/v;
                    if(d1>-sqrt(sqr(r0)-sqr(d2)))
                    {
                        d1-=sqrt(sqr(r0)-sqr(d2));
                        q0=(d1/v)/(r0/spd3);
                        if(qmin>q0)
                        {
                            qmin=q0;
                            //sprintf(tests,"dx=%.2f dy=%.2f d1=%.2f d2=%.2f",dx,dy,d1,d2);
                            if(abs(k->vx)>2*abs(k->vy))
                            {
                                if(k->vx*d2>0) dir=7;
                                else dir=3;
                            }
                            else if(abs(k->vy)>2*abs(k->vx))
                            {
                                if(k->vy*d2>0) dir=5;
                                else dir=1;
                            }
                            else if(k->vx*k->vy>0)
                            {
                                if(k->vx*d2>0) dir=6;
                                else dir=2;
                            }
                            else
                            {
                                if(k->vx*d2>0) dir=8;
                                else dir=4;
                            }
                        }
                        if(tmin>d1/v)
                        {
                            tmin=d1/v;
                        }
                    }
                }
            }
        }
        if(per==100.0)
        {
            boss=f.head.pre;
            dx=f.px-boss->x;
            dy=f.py-boss->y;
            dxc=abs(dx)-boss->r/3.0;
            dyc=abs(dy)-boss->r/3.0;
            if(dyc<cbhmin/2.0) tshoot=(dxc-cbwmax/2.0)/(cbspd-cbdw/2.0)*smul;
            else if(dxc<cbwmin/2.0) tshoot=(dyc-cbhmax/2.0)/(cbspd-cbdh/2.0)*smul;
            else tshoot=(dxc-cbwmin/2.0)/(cbspd)*smul+(dyc-cbhmin/2.0)/(cbspd)*smul;
            //sprintf(tests,"tshoot=%.2f tmin=%.2f",tshoot,tmin);
            if(tshoot+tshootd<tmin)
            {
                if(aikeydown[0])
                {
                    aikeydown[0]=0;
                    aikeydown[1]=0;
                }
                else
                {
                    aikeydown[1]=1;
                }
            }
            else
            {
                aikeydown[0]=1;
                aikeydown[1]=1;
            }
        }
        else
        {
            aikeydown[0]=1;
            aikeydown[1]=1;
        }
        if(dir==0)
        {
            boss=f.head.pre;
            dx=f.px-boss->x;
            dy=f.py-boss->y;
            if(sqr(dx)+sqr(dy)>tardist2)
            {
                if(abs(dx)>abs(dy)) if(dx>0) dir=1; else dir=5;
                else if(dy>0) dir=7; else dir=3;
            }
        }
    }
    aikeydown[2]=0;
    aikeydown[3]=f.px>mvl&&(dir==8||dir==1||dir==2);
    aikeydown[4]=f.px<mvr&&(dir==4||dir==5||dir==6);
    aikeydown[5]=f.py<mvt&&(dir==2||dir==3||dir==4);
    aikeydown[6]=f.py>mvb&&(dir==6||dir==7||dir==8);
}
